package com.liqi.web.controller.device;

import com.liqi.common.annotation.Log;
import com.liqi.common.constant.UserConstants;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.AjaxResult;
import com.liqi.common.core.page.TableDataInfo;
import com.liqi.common.enums.BusinessType;
import com.liqi.common.utils.DateUtils;
import com.liqi.common.utils.poi.ExcelUtil;
import com.liqi.common.utils.uuid.GenerateCodeUtils;
import com.liqi.device.domain.DvManufacturer;
import com.liqi.device.service.IDvManufacturerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 厂家Controller
 *
 * @author ruoyi
 * @date 2022-02-11
 */
@RestController
@RequestMapping("/device/manufacturer")
public class DvManufacturerController extends BaseController
{
    @Autowired
    private IDvManufacturerService dvManufacturerService;

    /**
     * 查询厂家列表
     */
    @PreAuthorize("@ss.hasPermi('device:manufacturer:list')")
    @GetMapping("/list")
    public TableDataInfo list(DvManufacturer dvManufacturer)
    {
        startPage();
        List<DvManufacturer> list = dvManufacturerService.selectDvManufacturerList(dvManufacturer);
        return getDataTable(list);
    }

    /**
     * 导出厂家列表
     */
    @PreAuthorize("@ss.hasPermi('device:manufacturer:export')")
    @Log(title = "厂家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DvManufacturer dvManufacturer)
    {
        List<DvManufacturer> list = dvManufacturerService.selectDvManufacturerList(dvManufacturer);
        ExcelUtil<DvManufacturer> util = new ExcelUtil<DvManufacturer>(DvManufacturer.class);
        util.exportExcel(response, list, "厂家数据");
    }

    /**
     * 获取厂家详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:manufacturer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dvManufacturerService.selectDvManufacturerById(id));
    }

    /**
     * 新增厂家
     */
    @PreAuthorize("@ss.hasPermi('device:manufacturer:add')")
    @Log(title = "厂家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DvManufacturer dvManufacturer)
    {
        dvManufacturer.setCode(GenerateCodeUtils.getSequnce());

        if (UserConstants.NOT_UNIQUE.equals(dvManufacturerService.checkCodeUnique(dvManufacturer)))
         {
             return AjaxResult.error("新增厂家" + dvManufacturer.getName() + "'失败，编码已存在");
         }

    	 dvManufacturer.setCreateBy(getUsername());
         dvManufacturer.setCreateTime(DateUtils.getNowDate());
         dvManufacturer.setUpdateBy(getUsername());
         dvManufacturer.setUpdateTime(DateUtils.getNowDate());
    	 return toAjax(dvManufacturerService.insertDvManufacturer(dvManufacturer));
    }

    /**
     * 修改厂家
     */
    @PreAuthorize("@ss.hasPermi('device:manufacturer:edit')")
    @Log(title = "厂家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DvManufacturer dvManufacturer)
    {
    	if (UserConstants.NOT_UNIQUE.equals(dvManufacturerService.checkCodeUnique(dvManufacturer)))
        {
            return AjaxResult.error("修改厂家" + dvManufacturer.getName() + "'失败，编码已存在");
        }
    	dvManufacturer.setUpdateBy(getUsername());
        dvManufacturer.setUpdateTime(DateUtils.getNowDate());
        return toAjax(dvManufacturerService.updateDvManufacturer(dvManufacturer));
    }

    /**
     * 删除厂家
     */
    @PreAuthorize("@ss.hasPermi('device:manufacturer:remove')")
    @Log(title = "厂家", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = 0;
        for (long id : ids) {
            DvManufacturer dvManufacturer = dvManufacturerService.selectDvManufacturerById(id);
            if (dvManufacturer != null ) {
                dvManufacturer.setUpdateBy(getUsername());
                dvManufacturer.setUpdateTime(DateUtils.getNowDate());
                dvManufacturer.setStatus(1);
                i = dvManufacturerService.updateDvManufacturer(dvManufacturer);
            }
        }
        return toAjax(i);
    }

    /**
     * 获取所有有效厂家 用于下拉列表
     */
    @GetMapping("/listDropdown")
    public AjaxResult listDropdown()	{
    	DvManufacturer dvManufacturer = new DvManufacturer();
    	dvManufacturer.setStatus(0);//0：有效； 1：停用
        List<DvManufacturer> dropDownItems = dvManufacturerService.selectDvManufacturerList(dvManufacturer);
        return AjaxResult.success(dropDownItems);
    }
}
